package com.simba.server.controllers.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.simba.server.components.CommandExecutor;
import org.apache.log4j.Logger;



import com.simba.common.log.LogUtil;

import com.simba.server.commands.impl.fetch.FetchMachineInfoCmd;
import com.simba.server.commands.impl.record.RecordMachineInfoCmd;
import com.simba.server.commands.impl.record.RecordMachineStatusLocalCmd;
import com.simba.server.commands.impl.update.RenewMachineInfoCmd;
import com.simba.server.commands.impl.web.WebBFJStatusCmd;
import com.simba.server.commands.impl.web.WebOBRStateCmd;
import com.simba.server.components.data.MachineRunInfo;
import com.simba.server.controllers.IMachineStatusController;

public class MachineStatusController implements IMachineStatusController
{
	private static final Logger logger = Logger.getLogger(MachineStatusController.class);
	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private String time;

	@Override
	public void onMachineStateMessage(int machineStatus)
	{
		try
		{
			this.handleMachineStatus(machineStatus);
		}
		catch (Exception ex)
		{
			LogUtil.LogError(MachineStatusController.logger,
					"Handle Machine Status Message Error -- Exception : " + ex);
		}
	}

	private void addMachineStatusLocal(MachineRunInfo info)
	{
		if (info.getBeginTime() == null)
		{
			return;
		}
		Map<String, Object> map = new HashMap<>();
		map.put("beginTime", info.getBeginTime());
		map.put("endTime", info.getEndTime());
		map.put("status", info.getStatus());

		CommandExecutor.executorSqlCommand(RecordMachineStatusLocalCmd.NAME, map,false);
	}

	private void handleMachineStatus(int machineStatus)
	{
		switch (machineStatus)
		{
			case 1:
				// 开机
				this.handleTurnOn();
				break;
			case 2:
				// 关机
				this.handleTurnOff("1");
				break;
			case 4:
				// 急停
				this.handleTurnOff("2");
				break;
		}
	}

	private void handleTurnOff(String type)
	{
		Map<String, Object> map = new HashMap<>();
		time = format.format(new Date());
		map.put("endTime", time);
		map.put("status", type);

		CommandExecutor.executorSqlCommand(RenewMachineInfoCmd.NAME, map,false);
		map.clear();

		MachineRunInfo info = new MachineRunInfo();
		CommandExecutor.executorSqlCommand(FetchMachineInfoCmd.NAME, info,false);

		this.sendMachineInfo(info);
		this.addMachineStatusLocal(info);
		//CommandExecutor.executorWebCommand(WebOBRStateCmd.NAME, "2",false);
	}

	private void handleTurnOn()
	{
		Map<String, Object> map = new HashMap<>();
		time = format.format(new Date());
		map.put("beginTime", time);
		map.put("status", 1);
		boolean flag = CommandExecutor.executorSqlCommand(RenewMachineInfoCmd.NAME, map,false);
		if (!flag)
		{
			CommandExecutor.executorSqlCommand(RecordMachineInfoCmd.NAME, map,false);
		}
		map.clear();
		//CommandExecutor.executorWebCommand(WebOBRStateCmd.NAME, "1",false);
	}

	private void sendMachineInfo(MachineRunInfo info)
	{
		//CommandExecutor.executorWebCommand(WebBFJStatusCmd.NAME, info,false);
	}
}
